#/usr/bin/bash
java -version

if [ $? -ne 0  ];then
	echo "--------------请先安装jdk,退出脚本------------"
	exit
fi

mvn -v 

if [ $? -ne 0 ];then
	 echo "--------------请先安装maven,退出脚本------------"
         exit
fi

service mysqld status

if [ $? -ne 0 ];then
	echo "--------------请确保mysql已经安装并且启动了,退出脚本------------"
        exit
fi

MYSQL_USER=$1
if [ ! $MYSQL_USER ];then
	MYSQL_USER=root
fi

MYSQL_PASSWORD=$2
if [ ! $MYSQL_PASSWORD ];then
	MYSQL_PASSWORD=123456
fi

mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -e "quit";

if [ $? -ne 0 ];then
        echo "mysql使用用户名$MYSQL_USER和密码$MYSQL_PASSWORD连接不上数据库，请检查是否有误，退出脚本。"
fi


INSTALL_PATH=$3
if [ ! $INSTALL_PATH ];then
	INSTALL_PATH="/usr/local/nacos"
fi

#Nacos架包下载路径,不传默认ftp://ftpUser:123456@175.178.190.189/nacos/nacos-server-2.1.0/nacos-server-2.1.0.tar.gz
DOWNLOAD_URL=$4
if [ ! $DOWNLOAD_URL ];then
DOWNLOAD_URL="ftp://ftpUser:123456@175.178.190.189/nacos/nacos-server-2.1.0/nacos-server-2.1.0.tar.gz"
fi

#Nacos端口
NACOS_PORT=$5
if [ ! $NACOS_PORT  ];then
	NACOS_PORT=8848
fi

#如果文件夹不存在，创建文件夹
if [ ! -d $INSTALL_PATH ];then
   mkdir $INSTALL_PATH
fi

cd $INSTALL_PATH

#wget下载文件
wget $DOWNLOAD_URL

##获取文件名
FILENAME=${DOWNLOAD_URL##*/};


#判断当前文件是什么压缩包
if [ ${FILENAME##*.} = gz ];then
  TEMP=${FILENAME%.*}
    if [ ${TEMP##*.} = tar ];then
        tar -zxvf $FILENAME -C $INSTALL_PATH
    else
        unzip  $FILENAME -d  $INSTALL_PATH
    fi
elif [ ${FILENAME##*.} = zip ];then
    unzip  $FILENAME -d  $INSTALL_PATH
fi


#获取解压后当前目录下的文件名
NEW_FILENAME=`ls -lt $INSTALLPATH |grep nacos|head -n 1|awk '{print $9}'`;


if [ ! $NEW_FILENAME ];then
  echo "解压失败，获取不到解压后的文件名，退出脚本";
  exit
fi

#删除安装包
rm -rf $INSTALL_PATH/$FILENAME

cd $NEW_FILENAME/bin

#备份
cp startup.sh startup_backup.sh

#修改jvm启动参数
sed -i "/^.*JAVA_OPT=\"\${JAVA_OPT} -server.*$/a JAVA_OPT=\"\${JAVA_OPT} -server -Xms512m -Xmx512m -Xmn256m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m\"" startup.sh
sed -i "N;/\n.*JAVA_OPT=\"\${JAVA_OPT} -server -Xms512m -Xmx512m -Xmn256m.*/!P;D" startup.sh

cd $INSTALL_PATH/$NEW_FILENAME/conf

expect << EOF
    set timeout -1spawn
    spawn mysql -u$MYSQL_USER -p$MYSQL_PASSWORD
    expect "*>*" {send "create database nacos;\n"}
    expect "*>*"  {send "use nacos;\n"}
    expect "*>*"  {send "source nacos-mysql.sql;\n"}
    expect "*>*" {send "quit;\n"}  
EOF

#修改nacos启动配置文件，添加数据库配置
if [ $NACOS_PORT -ne 8848 ];then
	sed -i "/^server.port=.*$/a  server.port=$NACOS_PORT" application.properties
	sed -i "N;/\nserver.port=$NACOS_PORT.*/!P;D" application.properties
fi

cat >>$INSTALL_PATH/$NEW_FILENAME/conf/application.properties<<EOF
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=$MYSQL_USER
db.password=$MYSQL_PASSWORD
EOF

cd $INSTALL_PATH/$NEW_FILENAME/bin

#单机启动nacos（standalone代表着单机模式运行，非集群模式）
sh startup.sh -m standalone

if [ $? -ne 0 ]; then
    echo "Nacos启动失败，请排查原因，退出脚本";
    exit;
else
    echo "Nacos启动成功，请稍后手动登录云服务器,在安全组中放通$NACOS_PORT端口";
fi
